package cn.ecnu.common;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//想把枚举类关系映射为整型，但是没用上
@MappedTypes(value = Relation.class)
public class RelationHandler implements TypeHandler<Relation> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Relation o, JdbcType jdbcType) throws SQLException {
        preparedStatement.setByte(i, (byte) o.ordinal());
    }

    @Override
    public Relation getResult(ResultSet resultSet, String s) throws SQLException {
        return Relation.values()[resultSet.getInt(s)];
    }

    @Override
    public Relation getResult(ResultSet resultSet, int i) throws SQLException {
        return Relation.values()[i];
    }

    @Override
    public Relation getResult(CallableStatement callableStatement, int i) throws SQLException {
        return Relation.values()[callableStatement.getInt(i)];
    }
}
